package com.deng.hadoop.mapreduce.demo.weather;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

/**
 * Created By DengLibin
 * Date: 19-4-3
 * Time: 上午10:51
 * Desc: reduce任务的分组比较器 （决定reduce输入的key按什么分组）
 */
public class WeatherGroupComparator extends WritableComparator {

    public WeatherGroupComparator(){
        super(TianQi.class, true);
    }

    /**
     * 分组规则 按年月分组
     * @param a
     * @param b
     * @return
     */
    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        TianQi t1 = (TianQi) a;
        TianQi t2 = (TianQi) b;
        int x = Integer.compare(t1.getYear(), t2.getYear());
        if(x == 0){ //年份相等
            x = Integer.compare(t1.getMonth(), t2.getMonth());//比较月份
        }
        return x;
    }
}
